﻿using System;
using System.Security.Cryptography;
using System.Text;
using System.Web;

namespace Maticsoft.DBUtility
{
    /// <summary>
    /// 加密加密连接字符串
    /// </summary>
    public static class DES
    {
        private const string UIKey = "ui&$%;fjlk;)*kcb(+nmjdsf$#@@#$&*dsf54641#&*(()";
        private const string passwordKey = "$%Gfdgs213)*(K::'[;jkjg#$FGDfgkhj&^ghdr;l;";
        private const string orderKey = "23$@#$GFdsfdsf%^#2346&EFGdfgW34.;p'..;'dSF324";

        private static string GetKey(string flag)
        {
            switch (flag)
            {
                case "order": return orderKey;
                case "ui": return UIKey;
                case "passwordKey": return passwordKey;
                default: return "";
            }
        }

        /// <summary>
        /// 3des加密字符串
        /// </summary>
        /// <param name="plainText">明文</param>
        /// <param name="encoding">编码方式</param>
        /// <returns>加密后并经base63编码的字符串</returns>
        /// <remarks>重载，指定编码方式</remarks>
        public static string Encrypt3DES(string plainText, Encoding encoding, string flag)
        {
            TripleDESCryptoServiceProvider DES = new
                TripleDESCryptoServiceProvider();
            MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();

            DES.Key = hashMD5.ComputeHash(encoding.GetBytes(GetKey(flag)));
            DES.Mode = CipherMode.ECB;

            ICryptoTransform DESEncrypt = DES.CreateEncryptor();

            byte[] Buffer = encoding.GetBytes(plainText);
            return Convert.ToBase64String(DESEncrypt.TransformFinalBlock
                                            (Buffer, 0, Buffer.Length));
        }


        #region  MD5加密方法
        /// <summary>
        /// 3des加密字符串
        /// </summary>
        /// <param name="plainText">明文</param>
        /// <param name="encoding">编码方式</param>
        /// <returns>加密后并经base63编码的字符串</returns>
        /// <remarks>重载，指定编码方式</remarks>
        //public static string EncryptMD5(string plainText, Encoding encoding, string flag)
        //{
        //    MD5CryptoServiceProvider md5=new MD5CryptoServiceProvider();
        //    md5.ComputeHash
        //}
        #endregion


        /// <summary>
        /// 3des解密字符串
        /// </summary>
        /// <param name="entryptText">密文</param>
        /// <param name="encoding">编码方式</param>
        /// <returns>解密后的字符串</returns>
        /// <remarks>静态方法，指定编码方式</remarks>
        public static string Decrypt3DES(string entryptText, Encoding encoding, string flag)
        {

            TripleDESCryptoServiceProvider DES = new
                TripleDESCryptoServiceProvider();
            MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();

            DES.Key = hashMD5.ComputeHash(encoding.GetBytes(GetKey(flag)));
            DES.Mode = CipherMode.ECB;

            ICryptoTransform DESDecrypt = DES.CreateDecryptor();
            try
            {
                byte[] Buffer = Convert.FromBase64String(entryptText);
                return encoding.GetString(DESDecrypt.TransformFinalBlock
                                                (Buffer, 0, Buffer.Length));
            }
            catch (System.Exception e)
            {
                throw (new System.Exception("Invalid Key or input string is not a valid base64 string", e));
            }
        }
    }
}
